//201902104030_何琛选
package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectTypeDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectTypeService {
	private static GraduateProjectTypeService graduateProjectTypeService = new GraduateProjectTypeService();
	private GraduateProjectTypeDao graduateProjectTypeDao = GraduateProjectTypeDao.getInstance();
	
	private GraduateProjectTypeService(){}
	
	public static GraduateProjectTypeService getInstance() throws SQLException{
		return graduateProjectTypeService;
	}

	public Collection< GraduateProjectType> findAll
			(List<Condition> conditionList_json_str, Pagination pagination)
			throws SQLException {
		Connection connection= JdbcHelper.getConn();
		Collection< GraduateProjectType> graduateProjectTypes =
				graduateProjectTypeDao.findAll(conditionList_json_str,pagination,connection);
		JdbcHelper.close(connection);
		return  graduateProjectTypes;
	}

	public GraduateProjectType find(Integer id)throws SQLException{
		Connection connection = JdbcHelper.getConn();
		GraduateProjectType result= graduateProjectTypeDao.find(id,connection);
		JdbcHelper.close(connection);
		return  result;
	}

	public boolean update(GraduateProjectType  graduateProjectType) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean result= graduateProjectTypeDao.update( graduateProjectType,connection);
		JdbcHelper.close(connection);
		return  result;
	}

	public boolean add( GraduateProjectType  graduateProjectType) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean result= graduateProjectTypeDao.add( graduateProjectType,connection);
		JdbcHelper.close(connection);
		return  result;
	}

	public boolean delete(Integer id) throws SQLException, bysjException {
		Connection connection = JdbcHelper.getConn();
		GraduateProjectType  graduateProjectType = this.find(id);
		boolean result= this.delete(graduateProjectType,connection);
		JdbcHelper.close(connection);
		return result;
	}


	public boolean delete(GraduateProjectType graduateProjectType_json,
						  Connection connection)
			throws SQLException,bysjException {
		boolean result=false;
		int countTeacherOfProfTitle = CommonDao.getInstance().
				countWhereForeignKey(connection,"graduateProject","graduateProjectType_id",graduateProjectType_json.getId());
		//若没有二级单位，则能够删除
		if(countTeacherOfProfTitle==0){
			result = graduateProjectTypeDao.delete( graduateProjectType_json,connection);
		}else {
			throw new bysjException( graduateProjectType_json.getDescription()+"有下属系，不能删除");
		}
		JdbcHelper.close(connection);
		return result;
	}
}
